<template>
    <div class="pageContainer">
        <el-container class="bg-white rounded-lg">
            <el-header class="flex items-center justify-between" style="border-bottom: 1px solid #eeeeee;">
                <el-button type="primary" @click="onAdd">新增</el-button>
                <el-button type="primary" text @click="onSearch">
                    <el-icon :size="20">
                        <Refresh />
                    </el-icon>
                </el-button>
            </el-header>
            <el-container>
                <div class="w-full">
                    <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName">
                        <el-table-column label="序号" width="90" align="center" fixed="left" prop="rank">
                            <template #default="{ $index, row }">
                                {{ (currentPage - 1) * limit + $index + 1 }}
                            </template>
                        </el-table-column>
                        <el-table-column prop="title" label="公告标题" min-width="400" />
                        <el-table-column label="发布时间" width="300">
                            <template #default="{ row }">
                                {{ dayjs(row.create_time).format('YYYY-MM-DD HH:mm:ss') }}
                            </template>
                        </el-table-column>
                        <el-table-column fixed="right" label="操作" min-width="120" align="center">
                            <template #default="scope">
                                <el-button link type="primary" @click="onEdit(scope.row)">修改</el-button>
                                <el-popconfirm title="确定删除该公告吗？" @confirm = "onDelete(scope.row)">
                                    <template #reference>
                                        <el-button type="primary" link >删除</el-button>
                                    </template>
                                </el-popconfirm>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div style="height: 50px;" class="flex items-center justify-center">
                        <el-pagination layout="prev, pager, next"  background :current-page="currentPage"
                            :total="totalCount" :page-size="10" @current-change="onPageChange" />
                    </div>
                </div>
            </el-container>
        </el-container>
        <FormDrawer ref="drawerRef" :title="drawerTitle" @submit="onSubmit">
            <el-form ref="formRef" :model="form" :rules="rules" label-width="100">
                <el-form-item label="公告名称" prop="title">
                    <el-input v-model="form.title" placeholder="请输入公告标题" clearable/>
                </el-form-item>
                <el-form-item label="公告内容" prop="content">
                    <el-input v-model="form.content" type="textarea" placeholder="请输入公告内容" :rows="5" clearable/>
                </el-form-item>
            </el-form>
        </FormDrawer>
    </div>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue'
import dayjs from 'dayjs'
import { getNoticeList, updateNoticeById, addNotice, deleteNoticeById } from '../../api/notice'
import { toast, openFullScreenLoading, closeFullScreenLoading } from '../../composables/util'
import FormDrawer from '../../components/FormDrawer.vue'
import {useTableSearch, useForm} from '../../composables/common'

// 查询相关
// 定义查询表单
const searchform = reactive({
    keyword: "",
})

// 定义清除查询条件的表单
const clearSearchform = () => {
    searchform.keyword = ''
}

const {
    tableRowClassName,
    onSearch,
    getData,
    onClearSearch,
    onPageChange,
    currentPage,
    tableData,
    totalCount,
    limit
} = useTableSearch({
    searchform,
    getTableList : getNoticeList,
    clearSearchform
})

// do not use same name with ref
const form = reactive({
    id: 0,
    title: '',
    content: '',
})

const rules = {
    title: [{ required: true, message: '公告名称不能为空', trigger: 'blur' }],
    content: [{ required: true, message: '公告内容不能为空', trigger: 'blur' }]
}

const {
    formRef,
    drawerRef,
    drawerTitle,
    onAdd,
    onEdit,
    onDelete,
    onSubmit
} = useForm({
    title: '公告', // 功能名称
    form, // form表单，必须
    tableData, // 表单数据来源
    getData, // 更新表单数据接口
    add: addNotice, // 新增接口
    update: updateNoticeById, // 修改接口
    delete: deleteNoticeById // 删除接口
})

</script>
